﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace engine
{
    /// <summary>
    /// SceneNode class
    /// Represents single object in scene graph
    /// </summary>
    public class SceneNode
    {
        public SceneNode ParentNode { get; set; }
        public Scene Scene { get { return scene; } }

        /// <summary>
        /// Node children list
        /// </summary>
        public List<SceneNode> Children { get { return children; } }

        /// <summary>
        /// Object transform matrix
        /// </summary>
        public Matrix Local { get; set; }

        public Matrix World { get; set; }

        protected List<SceneNode> children;
        protected Scene scene;

        /// <summary>
        /// Contructor
        /// Creates empty children list for scenenode
        /// </summary>
        public SceneNode() : this(new List<SceneNode>(), Matrix.Identity) { }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="children">List of children nodes</param>
        public SceneNode(List<SceneNode> children, Matrix transform)
        {
            this.children = new List<SceneNode>();
            if (children != null)
            {
                this.children.AddRange(children);
            }
            this.ParentNode = null;
            this.Local = transform;
        }

        public virtual void Initialize(Scene scene)
        {
            this.scene = scene;
        }

        public virtual void LoadContent()
        {

        }

        /// <summary>
        /// Draw method called by parent node
        /// </summary>
        /// <param name="gameTime">Current game time</param>
        /// <param name="world">Current world transform matrix(root transform matrix * ... * parent transform matrix)</param>
        public virtual void Draw(GameTime gameTime, Matrix parent)
        {
            World = parent * Local;
            // Drawing children
            foreach (SceneNode child in children)
            {
                child.Draw(gameTime, World);
            }
        }

        /// <summary>
        /// Update method called by parent node
        /// </summary>
        /// <param name="gameTime">Current game time received from Update method in OreganoGame class</param>
        public virtual void Update(GameTime gameTime)
        {
            // Invoking update method for children
            foreach (SceneNode child in children)
            {
                child.Update(gameTime);
            }
        }
    }
}
